package ellements;

import java.util.Random;

import ellements.drawable.Desk;
import sounds.MyMedia;
import matrix.world.volleyball.R;
import matrix.world.volleyball.MySurfaceView;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;


public class Ball extends Sprite{
	
	public Desk desk;
	
	public float x, y;
	private float vx, vy; 
	private final float radius=12*MySurfaceView.rate;
	private float angle;
	private boolean side=true,sidetop=true;
	private static final float DAMP = .99f;// he so ma sat
	private final float Gravity = 0.2f*MySurfaceView.rate*MySurfaceView.LEVEL;
	private final float Floor = 276f*MySurfaceView.rate;
	private final float BounceG = -0.95f;
	
	public static int ballLiveNumber = 10;
	
	private final float minVx = 0.1f*MySurfaceView.rate;
	
	/**
	 * bong cham san thi se nay len va se khong bao gio nay thap hon gia tri nay
	 */
	private final float minJumper = 6f*MySurfaceView.rate;
	/**
	 * am chi status ball da cham san
	 */
	private boolean isFloor = false;
	
	public Ball(Resources res) {
		Random rd = new Random();
		x=MySurfaceView.Width/2+rd.nextInt(MySurfaceView.Width/2);
		mBitmap = BitmapFactory.decodeResource(res, R.drawable.ball);
		
		Matrix matrix = new Matrix();
		matrix.postScale(MySurfaceView.rate, MySurfaceView.rate);
		mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
		
		pa.setAlpha(30);
	}
	
	public void animate(){
		angle++;
		vy+=Gravity;
		y+=vy;
		if (y>Floor){
			y=Floor;
			if (vy>minJumper || vy<-minJumper){
				MyMedia.playBall();
				vy*=BounceG;
			}else{
				vy=-minJumper;
			}
			updateScore();
		}else{
			isFloor = false;
		}
		x+=vx;
		if (vx<-minVx || vx>minVx) vx*=DAMP;
		collisionVolleyBallNet();
	}
	
 
	/**
	 * update score when ball down floor
	 */
    private void updateScore() {
    	if (!isFloor){
			isFloor = true;
			if (x>MySurfaceView.Width*0.5f)
				MySurfaceView.SCORE++;
			else{
				MySurfaceView.SCORE--;
				ballLiveNumber--;
				if (ballLiveNumber<=0){
					MySurfaceView.Status = MySurfaceView.GameOver;
					desk.updateSceneGameOver();
					return;
				}
			}
			desk.updateScene();
		}
	}
    
    
    
	private void collisionVolleyBallNet(){
		float dx = MySurfaceView.Width/2-x;
		float dy = 255*MySurfaceView.rate-y;
		if ((dx<Desk.dLeft && dx>-Desk.dRight) && Math.abs(dy)<Desk.dTop){
			MyMedia.playBall();
			if (sidetop){
    			y=Desk.NetTop;
    			vy*=BounceG;
    		}else 
    			if (side){
    				x=Desk.NetLeft;
    				vx=-vx;
    			}else{
    				x=Desk.NetRight;
    				vx=-vx;
    			}
		}else{
			side=x<MySurfaceView.Width/2;
			sidetop=y<Desk.NetTop;
			checkEdges();
		}
		
    	
    }
    
	protected void checkEdges() {
		if (x<0){
			x=0;
			vx*=-DAMP;
		}else if (x>MySurfaceView.Width){
			x=MySurfaceView.Width;
			vx*=-DAMP;
		}
	}
	
	public void collisionCrab(Crab crab){
		if (y>=crab.y) return;
		float dx = x-crab.x;
		float dy = y-crab.y;
		double a = Math.atan2(dy, dx);
		double d = Math.sqrt(dx*dx+dy*dy);
		if (d<radius+crab.radius){
			MyMedia.playBall();
			x=(float) (crab.x+(radius+crab.radius)*Math.cos(a));
			y=(float) (crab.y+(radius+crab.radius)*Math.sin(a));
			vx = (float) Math.cos(a)*crab.power;
			vy = (float) Math.sin(a)*crab.power;
		}
	}
	
	
	public void doDraw(Canvas c) {
        oval.left = x-radius*y/(268*MySurfaceView.rate);
        oval.top = 288*MySurfaceView.rate-4*y/268;
        oval.right = x+radius*y/(268*MySurfaceView.rate);
        oval.bottom = 288*MySurfaceView.rate+4*y/268;
        c.drawOval(oval, pa);
		c.save();
		c.rotate(angle, x, y);
		c.drawBitmap(mBitmap, x - radius, y - radius, null);
		c.restore();
	}
	
	
	
}
